<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fpaas_api%2Fnyvhmb&namespace=opendoc%2Fpaas_api
 * 	电子签名SaaS API非标版
 * 	机构签署账号API----接口顺序排列封装
 * 
 */


$appId = "7438873193";       //应用id
$secret = "1dc4d84aeeb9698c29c57202485581b5";  //应用密钥
$grantType = "client_credentials";   //授权类型，固定值
$url = "https://smlopenapi.esign.cn";  // url沙箱环境

$token = getTokenEnd($appId,$secret,$grantType);  // 直接返回token值


$header = array(                          // header请求头信息
        'X-AjaxPro-Method:ShowList',
        'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36',
        'X-Tsign-Open-App-Id:'.$appId,
		'Content-Type:application/json',
		'charset=UTF-8',
		'X-Tsign-Open-Token:'.$token
    );

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [createOrgan 根据创建人accountid创建机构签署账号]    有用    send_request
 * @param  [type] $thirdPartyUserId [创建机构账号的唯一标识。可将企业证件号、企业邮箱地址等作为此账号的唯一标识。]
 * @param  [type] $creator          [创建人个人账号id  创建个人签署账号接口返回的accountid]
 * @param  [type] $name             [组织机构名称]
 * @param  [type] $idType           [机构证件类型--统一社会信用代码]
 * @param  [type] $idNumber         [机构证件号]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回机构签署账号id] {"code":0,"message":"成功","data":{"orgId":"bc5809c4f24d43f28f322a815763b3cb"}
 */
function createOrgan($thirdPartyUserId,$creator,$name,$idType,$idNumber,$orgLegalIdNumber,$orgLegalName,$header,$url="https://smlopenapi.esign.cn"){
	$data = [
		"thirdPartyUserId"=>$thirdPartyUserId,
		"creator"=>$creator,
	    "name"=>$name,
	    "idType"=>$idType,
	    "idNumber"=>$idNumber,
	    "orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
	$header = $header;
	$get_url = $url."/v1/organizations/createByThirdPartyUserId";
	$res = send_request($get_url, $method = "POST", $data, $header);
	return $res['data'];
}                  
// $thirdPartyUserId = "15179798888";  //创建机构账号的唯一标识。可将企业证件号、企业邮箱地址等作为此账号的唯一标识。
// $creator = "a7070967395447c884c1715063b6aac7";    //创建人个人账号id  创建个人签署账号接口返回的accountid
// $name = "赣州刘颂晶网络";   //组织机构名称
// $idType = "CRED_ORG_USCC";  //机构证件类型
// $idNumber = "546512395648525649";  //机构证件号
// $orgLegalIdNumber = "360731199907050379";    //机构法定代表人证件号
// $orgLegalName = "liusongjing";  //机构法定代表人名称
// $a = createOrgan($thirdPartyUserId,$creator,$name,$idType,$idNumber,$orgLegalIdNumber,$orgLegalName,$header);
// var_dump($a);   //  "orgId":"aba7702a0fc64f7d99ee158e3767421a"
// die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [updateOrgan 根据机构账号id修改机构名称 法人证件号和法人代表名称]   有用   send_request
 * @param  [type] $orgId            [机构签署账号ID]
 * @param  [type] $name             [机构名称]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回修改后机构信息]
 */
function updateOrgan($orgId,$name,$orgLegalIdNumber,$orgLegalName,$header,$url="https://smlopenapi.esign.cn"){
	$get_url = $url."/v1/organizations/".$orgId;
	$data = [
		'name'=>$name,
		"orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
	$header = $header;
	$res = send_request($get_url, $method = "PUT", $data, $header);
	return $res['data'];
}
// $orgId = "a655bc4f5b2f444db2fa0b70612cd898";
// $name = '赣州刘颂晶网络111';
// $a = updateOrgan($orgId,$name,$orgLegalIdNumber,$orgLegalName,$header);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [updateOrganThirdId 根据机构唯一标识修改机构名称 法人证件号和法人代表名称]  有用   send_request
 * @param  [type] $thirdPartyUserId [机构唯一标识]
 * @param  [type] $name             [机构名称]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回修改后机构信息]
 */
function updateOrganThirdId($thirdPartyUserId,$name,$orgLegalIdNumber,$orgLegalName,$header,$url="https://smlopenapi.esign.cn"){
	$get_url = $url."/v1/organizations/updateByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$data = [
		'name'=>$name,
		"orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
	$header = $header;
	$res = send_request($get_url, $method = "POST", $data, $header);
	return $res['data'];
}
// $thirdPartyUserId = "15179798888";
// $name = '赣州刘颂晶网络222';
// $a = updateOrganThirdId($thirdPartyUserId,$name,$orgLegalIdNumber,$orgLegalName,$header);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectOrgan 根据机构orgId查找机构信息]     有用    get
 * @param  [type] $orgId  [机构签署账号ID]
 * @param  [type] $header [请求头]
 * @param  string $url    [请求地址]
 * @return [json]         [返回机构的信息]
 */
function selectOrgan($orgId,$header,$url="https://smlopenapi.esign.cn"){
	$url = $url."/v1/organizations/".$orgId;
	$head = $header;
	$res = get($url,$head);
	return $res;
}
// $orgId = "1596432fc156415087ec260eaf79d105";
// $a = selectOrgan($orgId,$header);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectOrganThirdId 根据创建机构账号的唯一标识查找机构信息]   有用   get
 * @param  [type] $thirdPartyUserId [创建机构账号的唯一标识]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回机构的信息]
 */
function selectOrganThirdId($thirdPartyUserId,$header,$url="https://smlopenapi.esign.cn"){
	$url = $url."/v1/organizations/getByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$head = $header;
	$res = get($url,$head);
	return $res;
}
// $thirdPartyUserId = "15179798888";
// $a = selectOrganThirdId($thirdPartyUserId,$header);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [deleteOrgan 根据机构orgid账号id注销]    有用    curlData
 * @param  [type] $orgId  [机构账号orgId]
 * @param  [type] $header [请求头]
 * @param  string $url    [请求地址]
 * @return [json]         [返回成功]
 */
function deleteOrgan($orgId,$header,$url="https://smlopenapi.esign.cn"){
	$url = $url."/v1/organizations/".$orgId;
	$header = $header;
	$res = curlData($url, $data='', $header, $method = 'DELETE', $type='json');
	return $res;
}
// $orgId = "bc5809c4f24d43f28f322a815763b3cb";
// $a = deleteOrgan($orgId,$header);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [deleteOrganThirdPartyUserId 根据机构账号唯一标识注销机构账号]    有用   curlData
 * @param  [type] $thirdPartyUserId [机构账号唯一标识]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回成功]
 */
function deleteOrganThirdPartyUserId($thirdPartyUserId,$header,$url="https://smlopenapi.esign.cn"){
	$url = $url."/v1/organizations/deleteByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$header = $header;
	$res = curlData($url, $data='', $header, $method = 'DELETE', $type='json');
	return $res;
}
// $thirdPartyUserId = "15179798888";
// $a = deleteOrganThirdPartyUserId($thirdPartyUserId,$header);
// var_dump($a);die;
